<%@page import="interface_layer.ErrorHandler"%>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<%@ page import="database_layer.*" %>
<%@ page import="java.util.*"%>
<%@ page import="processing_layer.*" %>
<%@ page errorPage="exceptionPage.jsp" %>
<%-- 
	This JSP represents all the courses that a user is registered to. Of course,
	this page is only accessible to students. Additionally, it is usually not
	accessed directly, but by a button in the user page.
	The page allows not only seeing his courses, but gives an option to
	unregister to any of them.
 --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255"/>
<link href="css/templatemo_style.css" rel="stylesheet" type="text/css" />
<title>Courses the student is registered to</title>
</head>
<body>
<jsp:include page="header_part_1.html"/>
            <div id="templatemo_menu">
                <ul>
                    <li><a href="index.jsp">Home</a></li>
                    <li><a href="about.jsp">About Us</a></li>
<%
                    	String username = (String) session.getAttribute("username");
                        if (null != username)
                        {
%>
                            <li><a href="userPage.jsp">Your profile</a></li>
		                    <li><a href="allCourses.jsp" class="current">Courses</a></li>
		                    <li><a href="allUsers.jsp">Users</a></li>
<%
                        }
%>
                </ul>    	
            </div> <!-- end of templatemo_menu -->
<jsp:include page="header_part_2.html"/>
<div id="content">
<%
	String userType = (String) session.getAttribute("userType");
	
	if (null == username || null == userType)
	{
		/* Not logged in = Not authorized */
		ErrorHandler.parseForErrors(Common.UNAUTHORIZED_OPERATION, response, null);
		return;
	}
	
	String viewedUser = request.getParameter("requestedUser");
	/* This means that the user checked his own profile */
	if (null == viewedUser)
	{
		viewedUser = username;
	}
	else
	{
		/* There's a user view request */
		if (!userType.equals(Common.adminType))
		{
			/* Only an admin can see the courses that an other use has registered to */
			ErrorHandler.parseForErrors(Common.UNAUTHORIZED_OPERATION, response, null);
			return;			
		}
	}
	
	ArrayList<Course> userRegisteredCourses = null;
	
	userRegisteredCourses = QueryDispatcher.getRegisteredCourses(viewedUser);
	if (null == userRegisteredCourses)
	{
		ErrorHandler.parseForErrors(Common.INTERNAL_DB_ERROR, response, null);
		return;
	}
	
	if (0 == userRegisteredCourses.size())
	{
		if (!viewedUser.equals(username))
		{
			/* The viewed user is not the current checker, means that either
			 * the user doesn't exist or he has no courses...
			 */
%>
		<i>Sorry, either the user hasn't registered to courses, or he doesn't exist anymore...</i>
<%
		}
		else
		{
			/* This is the user... */
%>
			<i>Sorry, you're not registered to any courses yet...</i>
<%	
		}
	}
	else
	{
		if (viewedUser.equals(username))
		{
			/* These lines are relevant only if the users checks its own page */
		
%>
			<b><i>
			This list contains all your <b>registered</b> courses<br/>
			You may choose to <u>Unregister</u> (You can always enter the course details and perform this action)<br/>
			</i></b>
<%
		}
		else
		{
%>
			<b><i>
			This list contains all the courses the user has registered to.
			</i></b>		
<%
		}
%>
		<br/>
		<table border="1">
<%
		for (Course c : userRegisteredCourses)
		{
%>
			<tr>
				<td class="coursesAndUsersTable" id="coursesAndUsersDescription">	
<%
			out.println("<b>Name</b>: " + c.name + "<br/>");
			out.println("<b>Credit pts</b>: " + c.creditPoints + "<br/>");
			out.println("<b>Group number</b>: " + c.groupNumber + "<br/>");
			out.println("<a href='courseDetails.jsp?course=" + c.name + "'>More about this course</a>");
%>
				</td>
<%
			if (viewedUser.equals(username))
			{
				/* We need this entry only if the user is looking at its own profile */
%>
				<td class="coursesAndUsersTable" id="coursesAndUsersManagement">
	
				<form method="get" action="CourseRegistration">
				<input type="hidden" name="course" value="<%= c.name %>"/>
				<input type="hidden" name="operation" value="<%= "Unregister" %>"/>
				<input type="submit" value="Unregister" />	
				</form>
				</td>
<%
			}
%>
			</tr>
<%
		}
	}
%>
	</table>
</div>
<jsp:include page="sidebar.jsp"/>
<jsp:include page="footer.html"/>
</body>
</html>